<?php

include_once '../@base/portal.php';
include_once '../login/login.php';

$model = $post->model;
$time = $post->time;
$user = $post->user;
$username = $user->name;
output(doSave($model, $time, $username));

function doSave($model, $time, $username)
{
    $sql = "SELECT * FROM oprec_state WHERE model = '{$model}' ORDER BY time DESC";
    // logfile('STATE_LOAD', $sql);
    $result = exec_sql($sql);
    $output = ['status' => -1, "user" => $username];
    if ($result) {
        if ($result[0]['time'] == $time) {
            $output = ['status' => 0, "user" => $username];
        } elseif ($result[0]['time'] > $time) {
            $output = ['status' => 1, 'content' => $result[0], "user" => $username];
        } elseif ($result[0]['time'] < $time) {
            $output = ['status' => -2, "user" => $username];
        }
    }
    // logfile('STATE_LOAD', json_encode($output, JSON_UNESCAPED_UNICODE));
    return $output;
}
